package com.punai.dataanalysis.epdatakb.service.NG001change1212;

import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisCbjxkh;
import com.punai.dataanalysis.epdatakb.mapper.NG001change1212.CbjxkhC1212Mapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001CbjxkhC1212Service {

    @Autowired
    private CbjxkhC1212Mapper cbjxkhC1212Mapper;

    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        String tabType = dataAnalysisVo.getTabType();
        String proStatusFilter = null;
        /**
         * 数据来源新中大库
         * select distinct a.Stat, b.descript from  project_table a left join stats b on a.Stat=b.stat
         * project_table.Stat 字段翻译
         * bid	投标项目
         * clo	竣工项目
         * end	工程归档
         * fz	封账项目
         * hit	中标项目
         * JW	竣工未结算
         * JY	竣工已结算
         * pau	停工项目
         * pcl	完工项目
         * stp	终止项目
         * sts	在建项目
         */
        if (StrUtil.equals(tabType,"zjgc")) {
            proStatusFilter = "sts";
        } else  if (StrUtil.equals(tabType,"jggc")){
            proStatusFilter = "clo";
        } else  if (StrUtil.equals(tabType,"jsgc")){
            proStatusFilter = "JY";
        }
        Map<String,Object> params = new HashMap<>();
        params.put("fgs",dataAnalysisVo.getFgs());
        params.put("month1",dataAnalysisVo.isMonth1());
        params.put("proStatusFilter",proStatusFilter);
        List<DataAnalysisCbjxkh> infos = cbjxkhC1212Mapper.getInfos(params);
        infos = new ArrayList<>(infos);
        List<JtczResultBo> bncb = cbjxkhC1212Mapper.bncb(params);
        List<JtczResultBo> bncbjgc = cbjxkhC1212Mapper.bncbjgc();
        List<JtczResultBo> jzcb = cbjxkhC1212Mapper.jzcb();
        Map<String, BigDecimal> bncbMap = bncb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> bncbjgcMap = bncbjgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> jzcbMap = jzcb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        for (DataAnalysisCbjxkh info : infos) {
            String pc = info.getPc();
            // 赋值
            info.setCurrYearCb(bncbMap.getOrDefault(pc,BigDecimal.ZERO));
            info.setCurrYearCbJgc(bncbjgcMap.getOrDefault(pc,BigDecimal.ZERO));
            info.setNzCbHj(jzcbMap.getOrDefault(pc,BigDecimal.ZERO));
            calcXjZb(info);
        }


        if (proStatusFilter == null) {
            DataAnalysisCbjxkh jgyjs = ztFz(infos, "在建项目");
            DataAnalysisCbjxkh jgwjs = ztFz(infos, "竣工未结算");
            DataAnalysisCbjxkh zjxm = ztFz(infos, "竣工已结算");
            DataAnalysisCbjxkh zj = zj(infos);
            zj.setProName("总合计");
            // 先按专业分组
            infos = zyFz(infos);
            infos.add(jgyjs);
            infos.add(jgwjs);
            infos.add(zjxm);
            infos.add(zj);
        } else if (StrUtil.equals(tabType,"zjgc")) {
            DataAnalysisCbjxkh jgyjs = ztFz(infos, "在建项目");
            infos.add(jgyjs);
        } else if (StrUtil.equals(tabType,"jggc")){
            DataAnalysisCbjxkh jgwjs = ztFz(infos, "竣工未结算");
            infos.add(jgwjs);
        } else if (StrUtil.equals(tabType,"jsgc")){
            DataAnalysisCbjxkh zjxm = ztFz(infos, "竣工已结算");
            infos.add(zjxm);
        }
        return infos;
    }

    private void calcXjZb(DataAnalysisCbjxkh kh) {
        // excel上 未标注算法 和之前的表单一样把
        BigDecimal qnYsHj = kh.getQnYsHj();
        BigDecimal qnYsJgc = kh.getQnYsJgc();
        BigDecimal qnCbHj = kh.getQnCbHj();
        BigDecimal qnLr = NumberUtil.subtract(qnYsHj, qnCbHj);
        kh.setQnLr(qnLr);
        kh.setQnLrl(NumberUtil.divideUseProportion(qnLr,2,NumberUtil.add(qnYsHj,qnYsJgc)));

        // 本年
        BigDecimal currYearCzYs = kh.getCurrYearCzYs();
        BigDecimal currYearCzSj = kh.getCurrYearCzSj();
        BigDecimal currYearCzJgc = kh.getCurrYearCzJgc();
        BigDecimal currYearCb = kh.getCurrYearCb();
        kh.setCurrYearCzXj(NumberUtil.adds(currYearCzYs,currYearCzSj,currYearCzJgc));
        BigDecimal currYearLr = NumberUtil.subtract(currYearCzYs, currYearCb);
        kh.setCurrYearGclr(currYearLr);
        kh.setCurrYearGclrl(NumberUtil.divideUseProportion(currYearLr,2,NumberUtil.add(currYearCzYs,currYearCzJgc)));

        // 累计工程利润
        BigDecimal nzYsHj = kh.getNzYsHj();
        BigDecimal nzYsJgc = kh.getNzYsJgc();
        BigDecimal nzCbHj = kh.getNzCbHj();
        BigDecimal nzLr = NumberUtil.subtract(nzYsHj, nzCbHj);
        kh.setKlGclr(nzLr);
        kh.setKlGclrl(NumberUtil.divideUseProportion(nzLr,2,NumberUtil.add(nzYsHj,nzYsJgc)));
    }

    private DataAnalysisCbjxkh ztFz(List<DataAnalysisCbjxkh> res,String proState ) {
        List<DataAnalysisCbjxkh> vals = res.stream().filter(e -> StrUtil.equals(e.getProStatus(), proState)).collect(Collectors.toList());
        DataAnalysisCbjxkh zj = zj(vals);
        zj.setProName(proState + "小计");
        return zj;
    }

    private DataAnalysisCbjxkh zj(List<DataAnalysisCbjxkh> res) {
        DataAnalysisCbjxkh zj = new DataAnalysisCbjxkh();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal jzmj = BigDecimal.ZERO;
        BigDecimal sgtYs = BigDecimal.ZERO;
        BigDecimal sgtJgc = BigDecimal.ZERO;
        BigDecimal qnYsHj = BigDecimal.ZERO;
        BigDecimal qnYsSj = BigDecimal.ZERO;
        BigDecimal qnYsJgc = BigDecimal.ZERO;
        BigDecimal qnCbHj = BigDecimal.ZERO;
        BigDecimal qnCbJgc = BigDecimal.ZERO;
        BigDecimal qnLr = BigDecimal.ZERO;
        BigDecimal nzYsHj = BigDecimal.ZERO;
        BigDecimal nzYsSj = BigDecimal.ZERO;
        BigDecimal nzYsJgc = BigDecimal.ZERO;
        BigDecimal nzCbHj = BigDecimal.ZERO;
        BigDecimal nzCbJgc = BigDecimal.ZERO;
        BigDecimal currYearCzYs = BigDecimal.ZERO;
        BigDecimal currYearCzJgc = BigDecimal.ZERO;
        BigDecimal currYearCzSj = BigDecimal.ZERO;
        BigDecimal currYearCzXj = BigDecimal.ZERO;
        BigDecimal currYearCb = BigDecimal.ZERO;
        BigDecimal currYearCbJgc = BigDecimal.ZERO;
        BigDecimal currYearGclr = BigDecimal.ZERO;
        BigDecimal klGclr = BigDecimal.ZERO;
        BigDecimal fzcCbZjPrice = BigDecimal.ZERO;
        for (DataAnalysisCbjxkh re : res) {
            htTotal = NumberUtil.add(htTotal,re.getHtTotal());
            jzmj = NumberUtil.add(jzmj,re.getJzmj());
            sgtYs = NumberUtil.add(sgtYs,re.getSgtYs());
            sgtJgc = NumberUtil.add(sgtJgc,re.getSgtJgc());
            qnYsHj = NumberUtil.add(qnYsHj,re.getQnYsHj());
            qnYsSj = NumberUtil.add(qnYsSj,re.getQnYsSj());
            qnYsJgc = NumberUtil.add(qnYsJgc,re.getQnYsJgc());
            qnCbHj = NumberUtil.add(qnCbHj,re.getQnCbHj());
            qnCbJgc = NumberUtil.add(qnCbJgc,re.getQnCbJgc());
            qnLr = NumberUtil.add(qnLr,re.getQnLr());
            nzYsHj = NumberUtil.add(nzYsHj,re.getNzYsHj());
            nzYsSj = NumberUtil.add(nzYsSj,re.getNzYsSj());
            nzYsJgc = NumberUtil.add(nzYsJgc,re.getNzYsJgc());
            nzCbHj = NumberUtil.add(nzCbHj,re.getNzCbHj());
            nzCbJgc = NumberUtil.add(nzCbJgc,re.getNzCbJgc());
            currYearCzYs = NumberUtil.add(currYearCzYs,re.getCurrYearCzYs());
            currYearCzJgc = NumberUtil.add(currYearCzJgc,re.getCurrYearCzJgc());
            currYearCzSj = NumberUtil.add(currYearCzSj,re.getCurrYearCzSj());
            currYearCzXj = NumberUtil.add(currYearCzXj,re.getCurrYearCzXj());
            currYearCb = NumberUtil.add(currYearCb,re.getCurrYearCb());
            currYearCbJgc = NumberUtil.add(currYearCbJgc,re.getCurrYearCbJgc());
            currYearGclr = NumberUtil.add(currYearGclr,re.getCurrYearGclr());
            klGclr = NumberUtil.add(klGclr,re.getKlGclr());
            fzcCbZjPrice = NumberUtil.add(fzcCbZjPrice,re.getFzcCbZjPrice());
        }

        zj.setHtTotal(htTotal);
        zj.setJzmj(jzmj);
        zj.setSgtYs(sgtYs);
        zj.setSgtJgc(sgtJgc);
        zj.setQnYsHj(qnYsHj);
        zj.setQnYsSj(qnYsSj);
        zj.setQnYsJgc(qnYsJgc);
        zj.setQnCbHj(qnCbHj);
        zj.setQnCbJgc(qnCbJgc);
        zj.setQnLr(qnLr);
        zj.setNzYsHj(nzYsHj);
        zj.setNzYsSj(nzYsSj);
        zj.setNzYsJgc(nzYsJgc);
        zj.setNzCbHj(nzCbHj);
        zj.setNzCbJgc(nzCbJgc);
        zj.setCurrYearCzYs(currYearCzYs);
        zj.setCurrYearCzJgc(currYearCzJgc);
        zj.setCurrYearCzSj(currYearCzSj);
        zj.setCurrYearCzXj(currYearCzXj);
        zj.setCurrYearCb(currYearCb);
        zj.setCurrYearCbJgc(currYearCbJgc);
        zj.setCurrYearGclr(currYearGclr);
        zj.setKlGclr(klGclr);
        zj.setFzcCbZjPrice(fzcCbZjPrice);
        zj.hb();
        calcXjZb(zj);
        return zj;
    }

    private List<DataAnalysisCbjxkh> zyFz(List<DataAnalysisCbjxkh> res) {
        List<DataAnalysisCbjxkh> newRes = new ArrayList<>();
        Map<String, List<DataAnalysisCbjxkh>> zyGroup = res.stream().filter(e -> StrUtil.isNotBlank(e.getZy())).collect(Collectors.groupingBy(DataAnalysisCbjxkh::getZy));

        for (Map.Entry<String, List<DataAnalysisCbjxkh>> zyEntry : zyGroup.entrySet()) {
            String zyKey = zyEntry.getKey();
            List<DataAnalysisCbjxkh> zyValue = zyEntry.getValue();
            // 在按项目部分组
            newRes.addAll(zyValue);
            Map<String, List<DataAnalysisCbjxkh>> xmbGroup = zyValue.stream().filter(e -> StrUtil.isNotBlank(e.getProDept())).collect(Collectors.groupingBy(DataAnalysisCbjxkh::getProDept));
            for (Map.Entry<String, List<DataAnalysisCbjxkh>> xmbEntry : xmbGroup.entrySet()) {
                String key = xmbEntry.getKey();
                List<DataAnalysisCbjxkh> value = xmbEntry.getValue();
                DataAnalysisCbjxkh jgyjs = ztFz(res, "在建项目");
                DataAnalysisCbjxkh jgwjs = ztFz(res, "竣工未结算");
                DataAnalysisCbjxkh zjxm = ztFz(res, "竣工已结算");
                newRes.add(jgyjs);
                newRes.add(jgwjs);
                newRes.add(zjxm);
                DataAnalysisCbjxkh zj = zj(value);
                zj.setProName(key + "小计");
                newRes.add(zj);
            }
            DataAnalysisCbjxkh zj = zj(zyValue);
            zj.setProName(zyKey + "小计");
            newRes.add(zj);
        }
        return newRes;
    }


}
